using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace UnityEditorHelper
{
    public static class EditorHelper
    {
        private static bool mEndHorizontal = false;
        private static GUIStyle mHeaderTextStyle = new GUIStyle();

        static public bool DrawHeader(string text, bool forceOn, bool minimalistic = true, string style = null)
        {
            return DrawHeader(text, forceOn, Color.white, minimalistic, style);
        }

        static public bool DrawHeader(string text, bool forceOn, Color color, bool minimalistic = true, string style = null)
        {
            if (!minimalistic) GUILayout.Space(3f);
            if (!forceOn) GUI.backgroundColor = new Color(0.8f, 0.8f, 0.8f);
            GUILayout.BeginHorizontal();
            GUI.changed = false;

            if (minimalistic)
            {
                if (forceOn) text = "\u25BC" + (char) 0x200a + text;
                else text = "\u25BA" + (char) 0x200a + text;

                GUILayout.BeginHorizontal();
                GUI.contentColor = EditorGUIUtility.isProSkin
                    ? new Color(1f, 1f, 1f, 0.7f)
                    : new Color(0f, 0f, 0f, 0.7f);
                if (string.IsNullOrEmpty(style))
                {
                    mHeaderTextStyle = "PreToolbar2";
                }
                else
                {
                    mHeaderTextStyle = style;
                }

                mHeaderTextStyle.normal.textColor = color;
                if (!GUILayout.Toggle(true, text, mHeaderTextStyle, GUILayout.MinWidth(20f))) forceOn = !forceOn;

                //if (!GUILayout.Toggle(true, text,"PreToolbar2", GUILayout.MinWidth(20f))) forceOn = !forceOn;
                GUI.contentColor = Color.white;
                GUILayout.EndHorizontal();
            }
            else
            {
                text = "<b><size=11>" + text + "</size></b>";
                if (forceOn) text = "\u25BC " + text;
                else text = "\u25BA " + text;
                if (string.IsNullOrEmpty(style))
                {
                    mHeaderTextStyle = "dragtab";
                }
                else
                {
                    mHeaderTextStyle = style;
                }

                mHeaderTextStyle.normal.textColor = color;
                if (!GUILayout.Toggle(true, text, mHeaderTextStyle, GUILayout.MinWidth(20f))) forceOn = !forceOn;
                //if (!GUILayout.Toggle(true, text, "dragtab", GUILayout.MinWidth(20f))) forceOn = !forceOn;
            }

            if (!minimalistic) GUILayout.Space(2f);
            GUILayout.EndHorizontal();
            GUI.backgroundColor = Color.white;
            if (!forceOn) GUILayout.Space(3f);
            return forceOn;
        }

        static public void BeginContents(bool minimalistic = true)
        {
            if (!minimalistic)
            {
                mEndHorizontal = true;
                GUILayout.BeginHorizontal();
                EditorGUILayout.BeginHorizontal("TextArea", GUILayout.MinHeight(10f));
            }
            else
            {
                mEndHorizontal = false;
                EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(10f));
                GUILayout.Space(10f);
            }

            GUILayout.BeginVertical();
            GUILayout.Space(2f);
        }

        /// <summary>
        /// End drawing the content area.
        /// </summary>
        static public void EndContents()
        {
            GUILayout.Space(3f);
            GUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();

            if (mEndHorizontal)
            {
                GUILayout.Space(3f);
                GUILayout.EndHorizontal();
            }

            GUILayout.Space(3f);
        }
    }
}